package com.fw.ui.view.settings.globalsetting.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.ErrorBean;
import com.fw.service.globalsetting.bean.GlobalSettingBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.ConfigurationBAO;
import com.fw.ui.bao.MasterDataBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.settings.globalsetting.EditGlobalSettingView;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class EditGlobalSettingSaveButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private ConfigurationBAO configurationBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private MasterDataBAO masterDataBAO;
    @Autowired
    private UICommonUtils uiCommonUtils;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	EditGlobalSettingView editGlobalSettingView = (EditGlobalSettingView) this.getBasedComponent();

	String payAtAllServiceInquiryPeriod = editGlobalSettingView.getTxtPayAtAllServiceInquiryPeriod().getValue();
	String transactionReportMaximumPeriodSearchAeonUser = editGlobalSettingView.getTxtTransactionReportMaximumPeriodSearchAeonUser()
		.getValue();
	String transactionReportMaximumPeriodSearchMerchantUser = editGlobalSettingView
		.getTxtTransactionReportMaximumPeriodSearchMerchantUser().getValue();
	String auditLogMaximumPeriodSearch = editGlobalSettingView.getTxtAuditLogMaximumPeriodSearch().getValue();

	GlobalSettingBean globalSettingBean = new GlobalSettingBean();
	globalSettingBean
		.setPayAtAllServiceInquiryPeriod(payAtAllServiceInquiryPeriod != null && payAtAllServiceInquiryPeriod.length() > 0 ? Integer
			.parseInt(payAtAllServiceInquiryPeriod) : 0);
	globalSettingBean.setTransactionReportMaximumPeriodSearchAeonUser(transactionReportMaximumPeriodSearchAeonUser != null
		&& transactionReportMaximumPeriodSearchAeonUser.length() > 0 ? Integer
		.parseInt(transactionReportMaximumPeriodSearchAeonUser) : 0);
	globalSettingBean.setTransactionReportMaximumPeriodSearchMerchantUser(transactionReportMaximumPeriodSearchMerchantUser != null
		&& transactionReportMaximumPeriodSearchMerchantUser.length() > 0 ? Integer
		.parseInt(transactionReportMaximumPeriodSearchMerchantUser) : 0);
	globalSettingBean
		.setAuditLogMaximumPeriodSearch(auditLogMaximumPeriodSearch != null && auditLogMaximumPeriodSearch.length() > 0 ? Integer
			.parseInt(auditLogMaximumPeriodSearch) : 0);

	ApplicationUI applicationUI = (ApplicationUI) editGlobalSettingView.getUI();
	sessionInfoUtils.setSessionInfoDetails(globalSettingBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	Map<String, Serializable> mapResult = this.configurationBAO.updateGlobalSetting(globalSettingBean);
	Map<String, Object> result = new HashMap<String, Object>();
	if (mapResult != null) {
	    List<ErrorBean> errorBeans = (List<ErrorBean>) mapResult.get(List.class.getName() + "." + ErrorBean.class.getName());
	    globalSettingBean = (GlobalSettingBean) mapResult.get(GlobalSettingBean.class.getName());
	    if (errorBeans != null && !errorBeans.isEmpty()) {
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, errorBeans);
	    } else {
		if (globalSettingBean != null) {

		    editGlobalSettingView.setGlobalSettingBean(globalSettingBean);

		    uiCommonUtils.setValueUIComponent(editGlobalSettingView.getTxtPayAtAllServiceInquiryPeriod(),
			    globalSettingBean.getPayAtAllServiceInquiryPeriod());
		    uiCommonUtils.setValueUIComponent(editGlobalSettingView.getTxtTransactionReportMaximumPeriodSearchAeonUser(),
			    globalSettingBean.getTransactionReportMaximumPeriodSearchAeonUser());
		    uiCommonUtils.setValueUIComponent(editGlobalSettingView.getTxtTransactionReportMaximumPeriodSearchMerchantUser(),
			    globalSettingBean.getTransactionReportMaximumPeriodSearchMerchantUser());
		    uiCommonUtils.setValueUIComponent(editGlobalSettingView.getTxtAuditLogMaximumPeriodSearch(),
			    globalSettingBean.getAuditLogMaximumPeriodSearch());

		    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(editGlobalSettingView);

		    result.put(MessageBAO.MESSAGE_INFO_TYPE, "com.fw.ui.view.settings.globalsetting.EditGlobalSettingView.message.updated");
		} else {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE,
			    "com.fw.ui.view.settings.globalsetting.EditGlobalSettingView.message.failed");
		}
	    }
	} else {
	    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "message.error.common");
	}
	return result;
    }

    @Override
    public boolean validate(ClickEvent event) {
	EditGlobalSettingView editPasswordPolicyView = (EditGlobalSettingView) this.getBasedComponent();
	try {

	    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(editPasswordPolicyView);

	    boolean payAtAllServiceInquiryPeriodValid = editPasswordPolicyView.getTxtPayAtAllServiceInquiryPeriod().isValid();
	    boolean transactionReportMaximumPeriodSearchAeonUserValid = editPasswordPolicyView
		    .getTxtTransactionReportMaximumPeriodSearchAeonUser().isValid();
	    boolean transactionReportMaximumPeriodSearchMerchantUserValid = editPasswordPolicyView
		    .getTxtTransactionReportMaximumPeriodSearchMerchantUser().isValid();
	    boolean auditLogMaximumPeriodSearchValid = editPasswordPolicyView.getTxtAuditLogMaximumPeriodSearch().isValid();

	    if (payAtAllServiceInquiryPeriodValid && transactionReportMaximumPeriodSearchAeonUserValid
		    && transactionReportMaximumPeriodSearchMerchantUserValid && auditLogMaximumPeriodSearchValid) {
		return true;
	    } else {
		if (!payAtAllServiceInquiryPeriodValid) {
		    editPasswordPolicyView.getTxtPayAtAllServiceInquiryPeriod().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtPayAtAllServiceInquiryPeriod().getErrorMessage() != null) {
			editPasswordPolicyView.getLblPayAtAllServiceInquiryPeriodValidateMessage().setValue(
				editPasswordPolicyView.getTxtPayAtAllServiceInquiryPeriod().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow1Validate().setVisible(true);
		}

		if (!transactionReportMaximumPeriodSearchAeonUserValid) {
		    editPasswordPolicyView.getTxtTransactionReportMaximumPeriodSearchAeonUser().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtTransactionReportMaximumPeriodSearchAeonUser().getErrorMessage() != null) {
			editPasswordPolicyView.getLblTransactionReportMaximumPeriodSearchAeonUserValidateMessage().setValue(
				editPasswordPolicyView.getTxtTransactionReportMaximumPeriodSearchAeonUser().getErrorMessage()
					.getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow2Validate().setVisible(true);
		}

		if (!transactionReportMaximumPeriodSearchMerchantUserValid) {
		    editPasswordPolicyView.getTxtTransactionReportMaximumPeriodSearchMerchantUser().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtTransactionReportMaximumPeriodSearchMerchantUser().getErrorMessage() != null) {
			editPasswordPolicyView.getLblTransactionReportMaximumPeriodSearchMerchantUserValidateMessage().setValue(
				editPasswordPolicyView.getTxtTransactionReportMaximumPeriodSearchMerchantUser().getErrorMessage()
					.getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow3Validate().setVisible(true);
		}

		if (!auditLogMaximumPeriodSearchValid) {
		    editPasswordPolicyView.getTxtAuditLogMaximumPeriodSearch().addStyleName("v-textfield-error");
		    if (editPasswordPolicyView.getTxtAuditLogMaximumPeriodSearch().getErrorMessage() != null) {
			editPasswordPolicyView.getLblAuditLogMaximumPeriodSearchValidateMessage().setValue(
				editPasswordPolicyView.getTxtAuditLogMaximumPeriodSearch().getErrorMessage().getFormattedHtmlMessage());
		    }
		    editPasswordPolicyView.getRow4Validate().setVisible(true);
		}
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
	return false;
    }

}
